package com.luuyi.actionchain.action;

import android.util.Log;

public abstract class AbsAction implements Action {

    private Action next;
    /**
     * 是否立即执行next
     * 用于区分同步和异步两种行为
     */
    private boolean doNextNow = true;

    @Override
    public Action setNext(Action action) {
        next = action;
        return this;
    }

    @Override
    public void doNext() {
        if (next != null) {
            next.start();
        }
    }

    @Override
    public abstract void handle();

    @Override
    public Action start() {
        Log.i("LUUYITEST", getClass().getSimpleName() + " start handle");
        handle();
        // 如果
        if (doNextNow) {
            doNext();
        }
        return this;
    }

    /**
     * 行为的结束是同步还是异步
     * 同步的行为结束后立即调用doNext()，无需手动doNext()
     * 异步行为需要自己在行为结束后调用doNext()
     *
     * @param isSync
     * @return
     */
    protected Action setIsSyncAction(boolean isSync) {
        doNextNow = isSync;
        return this;
    }
}
